package cn.edu.csmzxy.rk233x.auth_system.controller;

import cn.edu.csmzxy.rk233x.auth_system.service.SysUserRoleService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.edu.csmzxy.rk233x.auth_system.dto.ResultDTO;
import cn.edu.csmzxy.rk233x.auth_system.dto.UserDTO;
import cn.edu.csmzxy.rk233x.auth_system.entity.SysUser;
import cn.edu.csmzxy.rk233x.auth_system.service.SysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;

/**
 * @author wumxing
 * @date 2025/10/15 9:52
 * @description 用户管理接口
 */

@Tag(name = "用户管理接口", description = "用户CRUD及角色分配")
@RestController
@RequestMapping("/api/sys/user")
@SecurityRequirement(name = "JWT")
public class SysUserController {

    @Resource
    private SysUserService sysUserService;
    @Resource
    private SysUserRoleService sysUserRoleService;

    @Operation(summary = "分页查询用户", description = "需要user:list权限")
    @PreAuthorize("hasAuthority('user:list')")
    @GetMapping("/list")
    public ResultDTO<IPage<SysUser>> getUserList(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页条数") @RequestParam(defaultValue = "10") Integer pageSize,
            @Parameter(description = "用户名模糊查询") @RequestParam(required = false) String username) {

        Page<SysUser> page = new Page<>(pageNum, pageSize);
        IPage<SysUser> userPage = sysUserService.selectUserPage(page,username);
        for(SysUser u: userPage.getRecords()){
            if(u.getId()== null) continue;
            u.setRoleList(sysUserRoleService.getRolesByUserId(u.getId()));
        }
        return ResultDTO.success(userPage);
    }

    @Operation(summary = "新增用户", description = "需要user:add权限")
    @PreAuthorize("hasAuthority('user:add')")
    @PostMapping
    public ResultDTO<?> addUser(@Valid @RequestBody UserDTO userDTO) {
        sysUserService.addUser(userDTO);
        return ResultDTO.success("用户新增成功");
    }

    @Operation(summary = "编辑用户", description = "需要user:edit权限")
    @PreAuthorize("hasAuthority('user:edit')")
    @PutMapping("/{id}")
    public ResultDTO<?> updateUser(
            @Parameter(description = "用户ID") @PathVariable Long id,
            @Valid @RequestBody UserDTO userDTO) {
        sysUserService.updateUser(id, userDTO);
        return ResultDTO.success("用户修改成功");
    }

    @Operation(summary = "删除用户", description = "需要user:delete权限")
    @PreAuthorize("hasAuthority('user:delete')")
    @DeleteMapping("/{id}")
    public ResultDTO<?> deleteUser(@Parameter(description = "用户ID") @PathVariable Long id) {
        sysUserService.deleteUser(id);
        return ResultDTO.success("用户删除成功");
    }
}
